package org.willthink.controller.notice;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.willthink.service.notice.NoticeService;
import org.willthink.vo.notice.NoticeFormVO;
import org.willthink.vo.notice.NoticeVO;

@Controller
@RequestMapping("/notice")
public class NoticeController {
	
	private NoticeService service;
	
	public void setService(NoticeService service) {
		this.service = service;
	}

	@RequestMapping(value="/noticeList.think")
	public ModelAndView noticeList(HttpServletRequest request , HttpServletResponse response){
	
		ModelAndView mav = new ModelAndView();
		List<Map<String, String>> noticeList = null;
		
		mav.setViewName("notice/noticeList");
		
		try {
			noticeList = service.getNoticeList("1");
		} catch (SQLException e) {
			e.printStackTrace();
			return mav;
		}
		mav.addObject("noticeList", noticeList);

		return mav;
	}
	
	//새글을 등록하는 창을 띄운다.
	@RequestMapping(value="/noticeRegister.think")
	public String noticeRegister(HttpServletRequest request, HttpServletResponse response){
		
		return "notice/noticeRegister";
	}
	
	//새글을 등록한다.
	@RequestMapping(value="/noticeRegProc.think")
	public ModelAndView noticeRegProcess(HttpServletRequest request, HttpServletResponse response, NoticeFormVO vo){
		ModelAndView mav =  new ModelAndView();
		
		try {
			vo.setWriter("kwyang");
			service.insertNotice(vo);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		mav.setView(new RedirectView("/notice/noticeList.think"));
		return mav;
	}
	
}